/**
 * UIManager.as
 * Andy Jiang   
 * Fusion Digital Shanghai Co,Ltd. All Rights Reserved.
 * version 0.1
 */ 

package jiezi.ui
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Stage;
	import flash.events.Event;
	import jiezi.log.Logger;
	import jiezi.util.StringUtils;
  
	public class UIManager
	{
		private static var mInstance:UIManager;

		private var mRoot:Stage;
		private var mUIParent:Frame;
		private var mObjectCreators:Object = {};

		public static function getSingleton():UIManager 
		{
			if( mInstance == null )
			{
				mInstance = new UIManager();
			}
			return mInstance;
		}

		public function initialize(root:Stage):void
		{
			mRoot = root;
			mRoot.addEventListener(Event.RESIZE, this.onRootResized)
	  
			mUIParent = new Frame("UIParent")
			mUIParent.width = mRoot.stageWidth;
			mUIParent.height = mRoot.stageHeight;
			
			mRoot.addChild(mUIParent.sprite)
	  
			// init creators
			mObjectCreators["frame"] = Frame.creator
			mObjectCreators["scrollingmessageframe"] = ScrollingMessageFrame.creator
		}
	
		protected function onRootResized(event:Event):void
		{
			mUIParent.setSize( mRoot.stageWidth, mRoot.stageHeight )
			Logger.getSingleton().log("root has resized " + mUIParent.width + " " + mUIParent.height)
		}

		public function createFrame(type:String, name:String="", parent:Frame=null, template:String="" ):*
		{
			var created_frame:Frame = null
			var lower_type:String = type.toLowerCase()
			if ( name == "" ) name = StringUtils.generateRandomString(8);
			
			if( mObjectCreators[lower_type] )
			{
				created_frame = mObjectCreators[lower_type](name)
			}
			else
			{
				created_frame = new Frame(name)
			}
			
			//mRoot.addChild(created_frame.sprite)
			
			if ( parent != null )
			{
				parent.addChild(created_frame)
			}
			else
			{
				mUIParent.addChild(created_frame)
			}
			return created_frame
		}
	}
}
